var models = require('../mysql/mysql');
// var multiparty = require('multiparty')
const fs = require('fs')
/**
 * 上传文件接口
 * url: /test
 * method: post
 */
const getTest = async (req, res, next) => {
    const imgurl = 'upload/' + req.file.filename
	const {categoryId,product,shop,price,oldprice,putaway} = req.body
	console.log(imgurl);
	console.log(req.body);
	var sql = `insert into goods(product,shop,price,oldprice,putaway,category_id,img_url) 
	values("${product}","${shop}",${price},${oldprice},${putaway},${categoryId},'${imgurl}')`; 
	let data = await models(sql)

	if (data.length > 0) {
		res.send({
			code: 1,
			info: '添加成功',
		})
	} else {
		res.send({
			code: -1,
			info: '添加失败!',
		})
	}
	// // 接收参数
	// console.log(req.body);
    // let form = new multiparty.Form({uploadDir:'../upload'});
	// form.parse(req,(err,fields,files)=>{
	// 	console.log('fields',fields);
	// 	console.log('files',files);
    //     // console.log(File);
    //     // const file = new File([fields.blob], fileName, { type: fileType, lastModified: Date.now() });
    //     // console.log('file',file);
	// })
	// var sql = `select * from goods`; 
	// let data = await models(sql)

	// if (data.length > 0) {
	// 	res.send({
	// 		code: 1,
	// 		info: {
	// 			list: data,
	// 		},
	// 	})
	// } else {
	// 	res.send({
	// 		code: -1,
	// 		info: '商品列表为空!',
	// 	})
	// }

}
const getImg = (req, res, next)=>{
	let img = req.query.img
	fs.readFile(`public/upload/${img}`,(err,data)=>{
		if (err) {
			res.send('读取错误')
		} else {
			res.send(data)
		}
	})

}

module.exports = {getTest,getImg}